﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Drawing;
using Marbles.Core.Content.Assets;
using Marbles.Core.Content.Sites;
using System.Drawing.Drawing2D;

namespace Marbles.Core.Handlers
{
    public class ThumbnailHandler : IHttpHandlerBase
    {
        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            string name = string.Empty;
            string assetid = string.Empty;
            Int32 w = 64;
            Int32 h = 64;
            bool attachment = false;

            if (!string.IsNullOrEmpty(context.Request.Params["name"])) { name = context.Request.Params["name"]; }
            if (!string.IsNullOrEmpty(context.Request.Params["id"])) { assetid = context.Request.Params["id"]; }
            if (!string.IsNullOrEmpty(context.Request.Params["w"])) { w = Convert.ToInt32(context.Request.Params["w"]); }
            if (!string.IsNullOrEmpty(context.Request.Params["h"])) { h = Convert.ToInt32(context.Request.Params["h"]); }
            if (!string.IsNullOrEmpty(context.Request.Params["attachment"])) { attachment = Convert.ToBoolean(context.Request.Params["attachment"]); }

            Bitmap bmp = null;

            if (!string.IsNullOrEmpty(name))
            {
                context.Response.ContentType = "image/jpeg";
                bmp = GetThumbnailImage(name, w, h, System.Drawing.Color.White);
                bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                bmp.Dispose(); context.Response.Flush();
                return;
            }

            if (string.IsNullOrEmpty(assetid))
            {
                context.Response.ContentType = "text/html";
                context.Response.Write("Asset not found.");
                context.Response.End();
                return;
            }

            Asset asset = new Asset(SiteDefinition.GetCurrent().SiteId, assetid);
            if (!asset.IsImage)
            {
                context.Response.ContentType = "text/html";
                context.Response.Write("Not a Image.");
                context.Response.End();
                return;
            }

            context.Response.ContentType = "image/jpeg";
            bmp = asset.GetThumbnailImage(w, h, System.Drawing.Color.White);
            bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            bmp.Dispose(); context.Response.Flush();
        }

        private Size GetProportionalSize(Size szMax, Size szReal)
        {
            int nWidth;
            int nHeight;
            double sMaxRatio;
            double sRealRatio;

            if (szMax.Width < 1 || szMax.Height < 1 || szReal.Width < 1 || szReal.Height < 1)
                return Size.Empty;
            sMaxRatio = (double)szMax.Width / (double)szMax.Height;
            sRealRatio = (double)szReal.Width / (double)szReal.Height;
            if (sMaxRatio < sRealRatio)
            {
                nWidth = Math.Min(szMax.Width, szReal.Width);
                nHeight = (int)Math.Round(nWidth / sRealRatio);
            }
            else
            {
                nHeight = Math.Min(szMax.Height, szReal.Height);
                nWidth = (int)Math.Round(nHeight * sRealRatio);
            }
            return new Size(nWidth, nHeight);
        }

        public Bitmap GetThumbnailImage(string sourcefile, int width, int height, Color Background)
        {
            Bitmap bmp = null;
            using (Image img = Image.FromFile(HttpContext.Current.Server.MapPath(sourcefile)))
            {
                Size sz = GetProportionalSize(new Size(width, height), img.Size);
                bmp = new Bitmap(width, height);
                Graphics g = Graphics.FromImage(bmp);
                g.Clear(Background);
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.InterpolationMode = InterpolationMode.High;
                System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(((width - sz.Width) / 2), ((height - sz.Height) / 2), sz.Width, sz.Height);
                g.DrawImage(img, rectDestination, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
            }

            return bmp;
        }


        public string Verbs
        {
            get { return "GET"; }
        }

        public bool Validate
        {
            get { return false; }
        }

        public string Path
        {
            get { return "Thumbnail.axd"; }
        }

        public string Name
        {
            get { return this.GetType().Name; }
        }


    }


}